package com.deer.wms.ware.task.strategy.waveDetail;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.Wave.WaveDetail;
import com.deer.wms.ware.task.model.Wave.WaveMaster;
import com.deer.wms.ware.task.model.enums.WaveDetailEnum;
import com.deer.wms.ware.task.service.SoDetailService;
import com.deer.wms.ware.task.service.SoMasterService;
import com.deer.wms.ware.task.service.WaveDetailService;
import com.deer.wms.ware.task.service.WaveMasterService;
import com.deer.wms.ware.task.strategy.DetailStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Author: xu
 * @Date: 2022/08/01/17:17
 * @Description:
 */
@Component
public class Close implements DetailStrategy {
    @Autowired
    private SoDetailService soDetailService;

    @Autowired
    private WaveDetailService waveDetailService;

    @Autowired
    private SoMasterService soMasterService;
    @Autowired
    private CodeRuleService codeRuleService;


    @Autowired
    private WaveMasterService waveMasterService;

    //8 已完成
    @Override
    public Integer getState() {
    return     WaveDetailEnum.WAVE_STATE_CLOSE.getStatenum();
    }




    @Override
    public void allocation(SoDetail oldSoDetail, SoDetail newDetail, String waveCode) {
        if (ObjectUtil.isEmpty(newDetail)) {
            return;
        }
        //老单子已经关闭，新增明细行
        newDetail.setState(0);
        soDetailService.save(newDetail);
        WaveMaster waveMaster = waveMasterService.getOneByField("code", waveCode);
        WaveDetail  waveDetail = new WaveDetail(newDetail);
        waveDetail.setWaveDetailCode(codeRuleService.generateCodeByRedis("waveDetailCode"));
        waveDetail.setRob(newDetail.getRob());
        waveDetail.setQuantity(newDetail.getOrderQuantity());
        SoMaster soMaster = soMasterService.getOneByField("bill_no", newDetail.getBillNo());
        waveDetail.setSoMasterId(soMaster.getSoMasterId());
        //波次单信息
        waveDetail.setWaveMasterId(waveMaster.getWaveId());
        waveDetail.setWaveCode(waveMaster.getCode());
        waveDetail.setWareId(waveMaster.getWareId());
        waveDetail.setWareName(waveMaster.getWareName());
        waveDetailService.save(waveDetail);
        newDetail.setWaveDetailId(waveDetail.getWaveDetailId());
        soDetailService.updateById(newDetail);
        waveDetailService.saveOrUpdate(waveDetail);



    }
}
